package project.parser.ast.stats;

import project.environments.DynamicEnv;
import project.environments.StaticEnv;
import project.environments.Value;
import project.parser.ast.exps.Exp;
import project.parser.tokenizer.TokenType;

public class IfThenElse extends ConFlow {
	private final Stat elseStat;

	public IfThenElse(Exp cond, Stat ifStat) {
		this(cond, ifStat, null);
	}

	public IfThenElse(Exp cond, Stat body, Stat elseStat) {
		super(cond, body);
		this.elseStat = elseStat;
	}

	@Override
	public void typecheck(StaticEnv senv) {
		super.typecheck(senv);
		elseStat.typecheck(senv);
	}

	@Override
	public void evaluate(DynamicEnv denv) {
		Value condValue = cond.evaluate(denv);
		if (condValue.asBoolean())
			body.evaluate(denv);
		else if (elseStat != null)
			elseStat.evaluate(denv);
	}

	@Override
	public String condName() {
		return TokenType.IF.toString();
	}
}
